/*
 * BrightnessDialog.java
 *
 * Created on Sep 19, 2011, 8:00:06 PM
 */
package com.photoexpert;

import java.awt.Transparency;
import java.awt.image.RescaleOp;

/**
 *
 * @author PANYIXIN
 */
class BrightnessDialog extends javax.swing.JDialog {

    BrightnessDialog() {
        super(App.Instance(), true);
        initComponents();
        setLocation(200, 200);
        _slider.setValue(0);
        _value.setText("0");
        ((PreviewPane)_previewPane).setOp(null);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _slider = new javax.swing.JSlider();
        _valueLabel = new javax.swing.JLabel();
        _value = new javax.swing.JTextField();
        _previewLabel = new javax.swing.JLabel();
        _previewPane = PreviewPane.CreateInstance();
        _cancel = new javax.swing.JButton();
        _ok = new javax.swing.JButton();
        _channelLabel = new javax.swing.JLabel();
        _channel = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Brightness");
        setModal(true);
        setResizable(false);

        _slider.setMajorTickSpacing(20);
        _slider.setMinimum(-100);
        _slider.setMinorTickSpacing(10);
        _slider.setPaintLabels(true);
        _slider.setPaintTicks(true);
        _slider.setValue(0);
        _slider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _sliderStateChanged(evt);
            }
        });

        _valueLabel.setText("Value:");

        _value.setText("0");
        _value.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _valueFocusLost(evt);
            }
        });

        _previewLabel.setText("Preview:");

        _previewPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        _previewPane.setPreferredSize(new java.awt.Dimension(252, 192));

        javax.swing.GroupLayout _previewPaneLayout = new javax.swing.GroupLayout(_previewPane);
        _previewPane.setLayout(_previewPaneLayout);
        _previewPaneLayout.setHorizontalGroup(
            _previewPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );
        _previewPaneLayout.setVerticalGroup(
            _previewPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 190, Short.MAX_VALUE)
        );

        _cancel.setText("Cancel");
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        _ok.setText("OK");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        _channelLabel.setText("Channel:");

        _channel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Red", "Green", "Blue" }));
        _channel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _channelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_previewPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(_ok, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                            .addComponent(_cancel, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)))
                    .addComponent(_previewLabel)
                    .addComponent(_slider, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_channelLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_channel, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_valueLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_value, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_channelLabel)
                    .addComponent(_channel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_valueLabel)
                    .addComponent(_value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_previewLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_ok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_cancel))
                    .addComponent(_previewPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void preview(int value) {
        if(value == 0)
            _op = null;
        else {
            int channel = _channel.getSelectedIndex();
            if(channel == 0) // All
                _op = new RescaleOp(1f, value, null);
            else if(_implementor.getImage().getTransparency() == Transparency.OPAQUE) {
                float[] scales = {1f, 1f, 1f};
                float[] offsets = {0f, 0f, 0f};
                channel--;
                offsets[channel] = value;
                _op = new RescaleOp(scales, offsets, null);
            } else {
                float[] scales = {1f, 1f, 1f, 1f};
                float[] offsets = {0f, 0f, 0f, 0f};
                offsets[channel] = value;
                _op = new RescaleOp(scales, offsets, null);
            }
        }
        ((PreviewPane)_previewPane).setOp(_op);
    }

    private void _sliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__sliderStateChanged
        int value = _slider.getValue();
        _value.setText(new Integer(value).toString());
        preview(value);
    }//GEN-LAST:event__sliderStateChanged

    private void _valueFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__valueFocusLost
        int value = Integer.parseInt(_value.getText());
        _slider.setValue(value);
        preview(value);
    }//GEN-LAST:event__valueFocusLost

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        if(_op != null)
            _implementor.adjust(_op);
        setVisible(false);
        dispose();
    }//GEN-LAST:event__okActionPerformed

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event__cancelActionPerformed

    private void _channelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__channelActionPerformed
        _value.setText("0");
        _slider.setValue(0);
        preview(0);
    }//GEN-LAST:event__channelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _cancel;
    private javax.swing.JComboBox _channel;
    private javax.swing.JLabel _channelLabel;
    private javax.swing.JButton _ok;
    private javax.swing.JLabel _previewLabel;
    private javax.swing.JPanel _previewPane;
    private javax.swing.JSlider _slider;
    private javax.swing.JTextField _value;
    private javax.swing.JLabel _valueLabel;
    // End of variables declaration//GEN-END:variables
    private RescaleOp   _op = null;
    private Implementor _implementor = Implementor.Instance();
}
